package cn.lexuss.diceapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import cn.lexuss.diceapp.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var dice: Dice
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        init()
    }

    private fun init(){
        dice = Dice(6)
    }

    fun rollBtn(v: View){
        v.id
//        Toast.makeText(this, R.string.rollingTip, Toast.LENGTH_SHORT).show()
        val num = dice.roll() // 随机点数
        binding.rollNumber.text = num.toString()

        binding.luckyNumberTip.visibility = if (num == 6){ View.VISIBLE }else{ View.GONE }

        val imgID = when(num){
            1 -> R.drawable.dice_1
            2 -> R.drawable.dice_2
            3 -> R.drawable.dice_3
            4 -> R.drawable.dice_4
            5 -> R.drawable.dice_5
            else -> R.drawable.dice_6
        }
        binding.diceImg.setImageResource(imgID)
    }

    class Dice(private val maxNum: Int) {
        fun roll(): Int {
            return (1..maxNum).random()
        }
    }

}